<form role="form" class="epii" method="post" data-form="1" action="{url reservation bed }">
    <div class="form-group">
        <label>房间：</label>
        <select class="selectpicker" name="r[room_id]" id="room" data-size="5">
            {:options,$room_select,$info.room_id}
        </select>
    </div>
    <div class="form-group">
        <label>床位：</label>
        <select class="selectpicker" name="r[bed_id]" id="bed" data-size="5">
        </select>
    </div>
    <input type="hidden" name="id" value="{$id?0}">
    <div class="form-footer">
                <button type="submit" class="btn btn-primary">提交</button>
    </div>
</form>
<script>

    window.onEpiiInit(function() {
        // room下拉框改变时，更新bed下拉框
        $("#room").change(function(){
            let room_id = $(this).val();
            if (room_id == "") {
                $("#bed").html("<option value=''>请选择床位</option>");
            }
           room(room_id);
        });
        // generateCityOptions();
        let room_id=parseInt({$info.room_id});
        let bed_id=parseInt({$info.bed_id});
        if(!isNaN(room_id)&&!isNaN(bed_id)){
            room(room_id,bed_id)
        }
    });
    function room(room_id,bed_id=0){
        $.ajax({
            url: '?app=bed@getBedByRoom&room_id='+room_id,
            type: 'get',
            dataType:'json',
            success: function(data) {
                let option='';
                for (let i=0;i<data.length;i++){
                    if(bed_id>0&&bed_id==data[i].id){
                        option += "<option value='"+data[i].id+"' selected>"+data[i].name+"</option>"; // 创建一个新的<option>元素tion>元素的文本内容
                    }else{
                        option += "<option value='"+data[i].id+"'>"+data[i].name+"</option>"; // 创建一个新的<option>元素tion>元素的文本内容
                    }
                }
                $('#bed').empty();
                $('#bed').append(option);
                $('#bed').selectpicker('refresh');
            }
        });
    }

</script>
